/*
 * Copyright (C) 2010 Moduad Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cc.telecomdigital.tdstock.activity.groups.more.push.client;

import cc.telecomdigital.tdstock.Framework.UniqueItem.TDStockLog;
import android.os.Handler;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

/**
 * A listener class for monitoring changes in phone connection states.
 * 
 * @author Sehwan Noh (devnoh@gmail.com)
 */
public class PhoneStateChangeListener extends PhoneStateListener
{

	private static final String		TAG							= PhoneStateChangeListener.class
																	.getSimpleName();

	private final NotificationService	notificationService;

	/**
	 * 这个是城市信号覆盖最低要求，我们认定当转换后的信号强度小于-90不能通讯
	 */
	private final static int			NORMAL_Normal_MIN_SIGNAL_STRENGTH	= -180;

	public PhoneStateChangeListener(NotificationService notificationService)
	{
		this.notificationService = notificationService;
	}

	// Asu Signal Strength convert to dBM
	public int getConvertdBM(int asu)
	{
		return -133 + (2 * asu);
	}

	/**
	 * get net Signal Strength
	 */
	@Override
	public void onSignalStrengthChanged(int asu)
	{
		// TODO Auto-generated method stub
		super.onSignalStrengthChanged(asu);
		/*		Log.d(TAG, "net Strength is" + getConvertdBM(asu) + "of now");
				if (getConvertdBM(asu) < NORMAL_Normal_MIN_SIGNAL_STRENGTH) {
					Log.d(TAG, "网络很差");
				}*/

		if (getConvertdBM(asu) > NORMAL_Normal_MIN_SIGNAL_STRENGTH)
		{

		}
	}

	@Override
	public void onDataConnectionStateChanged(int state)
	{
		super.onDataConnectionStateChanged(state);
		TDStockLog.d(TAG, "onDataConnectionStateChanged()...");
		TDStockLog.d(TAG, "Data Connection State = " + getState(state));

		if (state == TelephonyManager.DATA_CONNECTED)
		{
			XmppManager.waiting = 0;
			//			if (null != notificationService.getXmppManager()
			//					&& null != notificationService.getXmppManager()
			//							.getConnection()) {
			Handler handler = new Handler();
			handler.postDelayed(new Runnable()
			{

				@Override
				public void run()
				{
					// TODO Auto-generated method stub
					notificationService.connect(TAG);
				}
			}, Constants.RECONNECTION_DELAY);
			//			}else{
			//				notificationService.startService(TAG);
			//			}
		}
	}

	private String getState(int state)
	{
		switch (state)
		{
			case 0: // '\0'
				return "DATA_DISCONNECTED";
			case 1: // '\001'
				return "DATA_CONNECTING";
			case 2: // '\002'
				return "DATA_CONNECTED";
			case 3: // '\003'
				return "DATA_SUSPENDED";
		}
		return "DATA_<UNKNOWN>";
	}

}
